package com.dzl.fee;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareOrderEnum;
import com.dzl.order.pojo.query.WelfareOrderQuery;
import com.dzl.order.pojo.vo.WelfareOrderSupplierVo;
import com.dzl.order.pojo.vo.WelfareOrderParentVo;
import com.dzl.order.service.WelfareOrderService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 财务销售流水控制层
 * @date 2021/9/18 14:13
 * @author caobucao
 */
@RestController
@RequestMapping("/salesFlow")
public class SalesFlowController extends BaseController {

    @Autowired
    private WelfareOrderService welfareOrderService;

    /**
     * 财务 - 分页查询销售流水
     * @date 2021/9/18 14:47
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.order.pojo.vo.WelfareSalesOrderFlowVo>
     */
    @GetMapping("/querySalesFlowOrderVoList")
    public PageUtils<WelfareOrderParentVo> querySalesFlowOrderVoList(WelfareOrderQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        //企业端当orderStatus传2待发货时候转成11查待发货+待打款状态的订单
        if(EmptyUtils.isNotEmpty(query.getOrderStatus()) && query.getOrderStatus()==WelfareOrderEnum.ORDER_STATUS_PAID.getCode()){
            query.setOrderStatus(11);
        }
        PageInfo<WelfareOrderParentVo> pageInfo = welfareOrderService.getOrderListPage(query);
        if(EmptyUtils.isNotEmpty(pageInfo) && EmptyUtils.isNotEmpty(pageInfo.getList())){
            pageInfo.getList().forEach(x->{
                if(EmptyUtils.isNotEmpty(x.getChildOrderVoList())){
                    x.getChildOrderVoList().forEach(y->{
                        if(y.getOrderStatus()==WelfareOrderEnum.ORDER_STATUS_PRE_PAID.getCode()){
                            y.setOrderStatus(WelfareOrderEnum.ORDER_STATUS_PAID.getCode());
                            y.setOrderStatusName(WelfareOrderEnum.ORDER_STATUS_PAID.getName());
                        }
                        Integer orderGoodsType = y.getOrderGoodsType();
                        if(orderGoodsType == CommonConstants.APP_GOODS_SOURCE.PLAFORM){
                            y.setSupplierName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                        }
                    });
                }
            });
        }
        return new PageUtils<>(pageInfo);
    }

    /**
     * 财务 - 分页查询销售流水顶部状态栏数量
     * @date 2021/9/18 15:39
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.JsonResult<java.util.List<java.lang.Integer>>
     */
    @GetMapping("/querySalesFlowOrderVoCount")
    public JsonResult<List<Integer>> querySalesFlowOrderVoCount(WelfareOrderQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        List<Integer> list = welfareOrderService.getOrderCount(query);
        List<Integer> list_ = new ArrayList<>();
        list_.add(list.get(0));
        list_.add(list.get(1));
        list_.add(list.get(2)+list.get(3));
        list_.add(list.get(4));
        list_.add(list.get(5));
        list_.add(list.get(6));
        return JsonResult.success(list_);
    }


    /**
     * 财务 - 导出流水--废弃
     * @date 2021/5/21 14:56
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.order.pojo.vo.WelfareSalesFlowVo>
     */
   /* @GetMapping("/exportSalesFlowVoListPage")
    public PageUtils<WelfareOrderParentVo> querySalesFlowVoListPage(WelfareSalesFlowQuery query) {
        PageInfo<WelfareSalesFlowVo> pageInfo = welfareOrderService.selectSalesFlowVoListPage(query);
        return new PageUtils<>(pageInfo);
    }
*/

    /**
     * 导出销售流水--商品维度，选中
     * @date 2021/9/23 17:20
     * @author caobucao
     * @param jsonObject 传入ids
     * @return void
     */
    @PostMapping("/exportSalesFlowVoList")
    public void exportSalesFlowVoList(@RequestBody JSONObject jsonObject, HttpServletResponse res, HttpServletRequest req) {
        String ids = jsonObject.getString("ids");
        WelfareOrderQuery query = new WelfareOrderQuery();
        query.setOrderIds(ids);
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        List<WelfareOrderSupplierVo> list = welfareOrderService.getOrderChildList(query);
        if(EmptyUtils.isNotEmpty(list)){
            list.forEach(x->{
                if(x.getOrderStatus()==WelfareOrderEnum.ORDER_STATUS_PRE_PAID.getCode()){
                    x.setOrderStatus(WelfareOrderEnum.ORDER_STATUS_PAID.getCode());
                    x.setOrderStatusName(WelfareOrderEnum.ORDER_STATUS_PAID.getName());
                }
                Integer orderGoodsType = x.getOrderGoodsType();
                if(orderGoodsType == CommonConstants.APP_GOODS_SOURCE.PLAFORM){
                   // x.setEnterpriseName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                    x.setSupplierName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                }
            });
        }
        welfareOrderService.exportSaleFlow(list, res, req);
    }

    /**
     * 导出销售流水--商品维度，全部导出
     * @date 2021/9/23 17:20
     * @author caobucao
     * @param query 查询条件
     * @return void
     */
    @GetMapping("/exportAllSalesFlowVoList")
    public void exportAllSalesFlowVoList(WelfareOrderQuery query, HttpServletResponse res, HttpServletRequest req) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        if(EmptyUtils.isNotEmpty(query.getOrderStatus()) && query.getOrderStatus()== WelfareOrderEnum.ORDER_STATUS_PAID.getCode()){
            query.setOrderStatus(11);
        }
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        List<WelfareOrderSupplierVo> list = welfareOrderService.getOrderChildList(query);
        if(EmptyUtils.isNotEmpty(list)){
            list.forEach(x->{
                if(x.getOrderStatus()==WelfareOrderEnum.ORDER_STATUS_PRE_PAID.getCode()){
                    x.setOrderStatus(WelfareOrderEnum.ORDER_STATUS_PAID.getCode());
                    x.setOrderStatusName(WelfareOrderEnum.ORDER_STATUS_PAID.getName());
                }
                Integer orderGoodsType = x.getOrderGoodsType();
                if(orderGoodsType == CommonConstants.APP_GOODS_SOURCE.PLAFORM){
                  //  x.setEnterpriseName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                    x.setSupplierName(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_NAME);
                }
            });
        }
        welfareOrderService.exportSaleFlow(list, res, req);
    }



}
